import db from '../db/index.js'
export async function getclasslist(req, res) {
    const sql = 'select submajorid,name from submajor'
    const submajorclass = await new Promise((resolve, reject) => {
        db.query(sql, (err, data) => {
            if (err) return reject(err)
            resolve(data)
        })
    })
    const subids = submajorclass.map(item => item.submajorid)
    const sql2 = 'select cname ,classid,submajorid from class where submajorid in (?)'
    const classlist = await new Promise((resolve, reject) => {
        db.query(sql2, [subids], (err, data) => {
            if (err) return reject(err)
            resolve(data)
        })
    })
    submajorclass.forEach(item => {
        item.class = []
        classlist.forEach(cl => {
            if (cl.submajorid == item.submajorid) item.class.push(cl)
        })
    });
    res.send({ status: 200, data: submajorclass })
}
export function classinfo(req, res) {
    const { condition, val } = req.body
    const sql = `select classid,cname,introduce,submajorid from class where ${condition} =?`
    db.query(sql, val, (err, data) => {
        if (err) return res.dbno(err)
        if (data.length == 0) return res.cc('不存在该数据')
        const classdata = data[0]
        const sql2 = 'select honor,create_time from chonor where classid =?'
        db.query(sql2, classdata.classid, (err, data) => {
            if (err) return res.dbno(err)
            const honor = data
            const sql3 = 'select count(*) as sum from student where classid =? '
            db.query(sql3, classdata.classid, (err, data) => {
                if (err) return res.dbno(err)
                if (data.length == 0) return res.cc('不存在该数据')
                const sum = data[0].sum
                const sql4 = 'select teacher.name as teacher from submajor  inner join teacher on teacher.teacherid = submajor.teacherid where submajorid = ? '
                db.query(sql4, classdata.submajorid, (err, data) => {
                    if (err) return res.dbno(err)
                    if (data.length == 0) return res.cc('不存在该数据')
                    res.send({ status: 200, class: { ...classdata, sum, teacher: data[0].teacher }, honor })
                })

            })
        })
    })
}
export function getclassbysubid(req, res) {
    const { subid } = req.query
    const sql1 = 'select name ,submajorid from submajor where submajorid = ?'
    db.query(sql1, [subid], (err, data) => {
        if (err) return res.dbno(err)
        const subclass = data
        const sql = 'select  cname ,classid,submajorid from class where submajorid = ?'
        db.query(sql, [subid], (err, data) => {
            if (err) return res.dbno(err)
            subclass.class = data
            subclass.forEach(item => {
                item.class = []
                data.forEach(cl => {
                    if (cl.submajorid == item.submajorid) item.class.push(cl)
                })
            });
            res.send({ status: 200, data: subclass })
        })
    })


}